import sys

import facade
from xjlibrary.database_moudel.simple.mysqlclient import MySqlDbConnect, SelctSqlFromDB, ExeSqlList, ExeSqlToDB
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
mysqlutils = facade.MysqlUtiles(BaseDir.get_new_path(curPath, "db.ini"), "db", facade.get_streamlogger())


if __name__ == "__main__":
    sql = "SELECT rawid FROM modify_title_info_zt"
    rows = mysqlutils.SelectFromDB(sql)
    listrawid = []
    for row in rows:
        listrawid.append(row[0])
    while True:
        sql = "select url from archive where stat != 3 limit 10000"
        rows = mysqlutils.SelectFromDB(sql)
        dicts = {}
        listid = []
        listurl = []
        for row in rows:
            url = row[0]
            id = row[0].split("/")[-1]
            dicts[id] = url
            listid.append(id)
            listurl.append(url)
            # listrawid.append(row[0])
        intersection = list(set(listid).intersection(set(listrawid)))
        print(len(intersection))
        if len(intersection) == 10000:
            sql = "update archive set `stat`=3 where url in {}".format(tuple(listurl))
            mysqlutils.ExeSqlToDB(sql)
        else:
            listurl2 = []
            for id in intersection:
                listurl2.append(dicts[id])
            sql = "update archive set `stat`=3 where url in {}".format(tuple(listurl2))
            mysqlutils.ExeSqlToDB(sql)

    #
    # for rowid in listrawid:
    #     print(rowid)
    #     sql = "UPDATE archive SET stat = 3 WHERE url like '%{}%' and batch is null AND stat != 3"
    #     sql = sql.format(rowid)
    #     listSql.append(sql)
    #     if len(listSql) > 3000:
    #         InsertIntoDbFromList(listSql)
    #         listSql = list()
    # if len(listSql) > 0:
    #     InsertIntoDbFromList(listSql)
    #     listSql = list()
    # print("完成")
